package com.example.a1.fannong.adapter;

import android.content.Context;
import android.content.DialogInterface;
import android.support.v7.app.AlertDialog;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.ImageButton;
import android.widget.TextView;

import com.example.a1.fannong.R;
import com.example.a1.fannong.core.app.BaseListAdapter;
import com.example.a1.fannong.model.ShoppingCarEntity;

import java.util.List;

import cn.personal.library.imageloader.view.AsyncImageView;
import cn.personal.library.viewinject.FindViewById;
import cn.personal.library.viewinject.ViewInjecter;

/**
 * Created by 1 on 2017/10/18.
 */

public class ShoppingCartAdapter extends BaseListAdapter<ShoppingCarEntity> {

    private boolean isShow = true;//是否显示编辑/完成

    private CheckInterface checkInterface;
    private ModifyCountInterface modifyCountInterface;

    public ShoppingCartAdapter(Context context) {
        super(context);
    }

    public ShoppingCartAdapter(Context context, List<ShoppingCarEntity> items) {
        super(context, items);
    }

    @Override
    public View newView(LayoutInflater inflater, int position, ShoppingCarEntity item, ViewGroup parent) {
        View view = View.inflate(getContext(), R.layout.item_shopping_cart, null);
        ViewHolder viewHolder = new ViewHolder();
        ViewInjecter.inject(viewHolder, view);
        view.setTag(viewHolder);
        return view;
    }

    @Override
    public void bindView(View view, final int position, final ShoppingCarEntity item) {
        final ViewHolder viewHolder = (ViewHolder) view.getTag();
        viewHolder.title.setText(item.getName());
        viewHolder.numTv.setText(item.getCount()+"");
        viewHolder.priceTv.setText(item.getPrice());
        viewHolder.specsTv.setText(item.getSpec());
        viewHolder.mSelectRb.setChecked(item.isChoosed());

        viewHolder.mSelectRb.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                item.setChoosed(((CheckBox) v).isChecked());
                checkInterface.checkGroup(position, ((CheckBox) v).isChecked());
            }
        });
        viewHolder.addImage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                modifyCountInterface.doIncrease(position, viewHolder.numTv,viewHolder.mSelectRb.isChecked() );
            }
        });

        viewHolder.deleteImage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                modifyCountInterface.doDecrease(position,viewHolder.numTv,viewHolder.mSelectRb.isChecked());
            }
        });

        viewHolder.deleteTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AlertDialog alert = new AlertDialog.Builder(getContext()).create();
                alert.setTitle("操作提示");
                alert.setMessage("您确定要将这些商品从购物车中移除吗？");
                alert.setButton(DialogInterface.BUTTON_NEGATIVE, "取消",
                        new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                return;
                            }
                        });
                alert.setButton(DialogInterface.BUTTON_POSITIVE, "确定",
                        new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                modifyCountInterface.childDelete(position);//删除 目前只是从item中移除
                            }
                        });
                alert.show();
            }
        });

        if (isShow){

        }

    }

    public CheckInterface getCheckInterface() {
        return checkInterface;
    }

    /**
     * 是否显示可编辑
     *
     * @param flag
     */
    public void isShow(boolean flag) {
        isShow = flag;
        notifyDataSetChanged();
    }

    /**
     * 单选接口
     *
     * @param checkInterface
     */
    public void setCheckInterface(CheckInterface checkInterface) {
        this.checkInterface = checkInterface;
    }

    public ModifyCountInterface getModifyCountInterface() {
        return modifyCountInterface;
    }


    /**
     * 改变商品数量接口
     *
     * @param modifyCountInterface
     */
    public void setModifyCountInterface(ModifyCountInterface modifyCountInterface) {
        this.modifyCountInterface = modifyCountInterface;
    }

    public static class ViewHolder {
        @FindViewById(R.id.title_tv)
        public TextView title;
        @FindViewById(R.id.price_tv)
        public TextView priceTv;
        @FindViewById(R.id.num_et)
        public TextView numTv;
        @FindViewById(R.id.delete)
        public TextView deleteTv;
        @FindViewById(R.id.specs_tv)
        public TextView specsTv; //规格
        @FindViewById(R.id.select_rb)
        public CheckBox mSelectRb;
        @FindViewById(R.id.add_iamge)
        private ImageButton addImage;
        @FindViewById(R.id.delete_image)
        private ImageButton deleteImage;
        @FindViewById(R.id.goods_image)
        public AsyncImageView goodsImage;
    }

    public interface CheckInterface {
        /**
         * 组选框状态改变触发的事件
         * @param position  元素位置
         * @param isChecked 元素选中与否
         */
        void checkGroup(int position, boolean isChecked);
    }

    public interface ModifyCountInterface {
        /**
         * 增加操作
         *
         * @param position      组元素位置
         * @param showCountView 用于展示变化后数量的View
         * @param isChecked     子元素选中与否
         */
        void doIncrease(int position, View showCountView, boolean isChecked);

        /**
         * 删减操作
         *
         * @param position      组元素位置
         * @param showCountView 用于展示变化后数量的View
         * @param isChecked     子元素选中与否
         */
        void doDecrease(int position, View showCountView, boolean isChecked);

        /**
         * 删除子item
         *
         * @param position
         */
        void childDelete(int position);
    }
}
